package cre.tomcat.ch04.core;

import cre.tomcat.ch03.Constants;
import cre.tomcat.util.LoaderUtils;
import org.apache.catalina.*;

import javax.naming.directory.DirContext;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyChangeListener;
import java.io.IOException;

/**
 * @Author penglai
 * @Date 2017/1/23.
 */
public class SimpleContainer implements Container {


    public String getInfo() {
        return null;
    }

    public org.apache.catalina.Loader getLoader() {
        return null;
    }

    public void setLoader(org.apache.catalina.Loader loader) {

    }

    public Logger getLogger() {
        return null;
    }

    public void setLogger(Logger logger) {

    }

    public Manager getManager() {
        return null;
    }

    public void setManager(Manager manager) {

    }

    public Cluster getCluster() {
        return null;
    }

    public void setCluster(Cluster cluster) {

    }

    public String getName() {
        return null;
    }

    public void setName(String name) {

    }

    public Container getParent() {
        return null;
    }

    public void setParent(Container container) {

    }

    public ClassLoader getParentClassLoader() {
        return null;
    }

    public void setParentClassLoader(ClassLoader parent) {

    }

    public org.apache.catalina.Realm getRealm() {
        return null;
    }

    public void setRealm(org.apache.catalina.Realm realm) {

    }

    public DirContext getResources() {
        return null;
    }

    public void setResources(DirContext resources) {

    }

    public void addChild(Container child) {

    }

    public void addContainerListener(org.apache.catalina.ContainerListener listener) {

    }

    public void addMapper(Mapper mapper) {

    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {

    }

    public Container findChild(String name) {
        return null;
    }

    public Container[] findChildren() {
        return new Container[0];
    }

    public org.apache.catalina.ContainerListener[] findContainerListeners() {
        return new org.apache.catalina.ContainerListener[0];
    }

    public Mapper findMapper(String protocol) {
        return null;
    }

    public Mapper[] findMappers() {
        return new Mapper[0];
    }

    public void invoke(Request request, Response response) throws IOException, ServletException {
        System.out.println("in SimpleContainer invoke");

        String uri = ((HttpServletRequest) request).getRequestURI();
        String servletName = gainServletName(uri);

        ClassLoader loader = LoaderUtils.gainClassLoader();
        Class sClass = null;
        try {
            sClass = loader.loadClass(Constants.SERVLET_CLASS_PACKAGE + servletName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Servlet servlet = null;
        try {
            servlet = (Servlet) sClass.newInstance();
            servlet.service((HttpServletRequest) request, (HttpServletResponse)response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Container map(Request request, boolean update) {
        return null;
    }

    public void removeChild(Container child) {

    }

    public void removeContainerListener(org.apache.catalina.ContainerListener listener) {

    }

    public void removeMapper(Mapper mapper) {

    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {

    }

    private String gainServletName(String uri) {
        return uri.substring("/servlet".length() + 1);
    }


}
